package com.it.controller;

import com.it.pojo.*;
import com.it.service.CandidateService;
import com.it.service.StudentService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private CandidateService candidateService;
    @Autowired
    private StudentService studentService;

    @RequestMapping("/start")//选人方法
    public Student candidate(Integer classes, Integer team){
        return candidateService.candidate(classes, team);
    }

    @RequestMapping("/login")
    public Admin login(String username, String password, HttpServletRequest request) {
        Admin admin = studentService.login(username, password);
        if (admin != null) {
            request.getSession().setAttribute("admin", admin);
            return admin;
        } else {
            return null;
        }
    }

    @RequestMapping("/getall")
    public List<Student> getAll(Integer cid, Integer tid){
        return studentService.getAll(cid, tid);
    }

    @RequestMapping("/add")
    public String add(@RequestBody Student s){
        if (s.getTid() == null) {
            return "请选择小组";
        } else {
            studentService.add(s);
            return null;
        }
    }

    @RequestMapping("/update")
    public String update(@RequestBody Student s){
        if (s.getTid() == null) {
            return "请选择小组";
        } else {
            studentService.update(s);
            return null;
        }
    }

    @RequestMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        studentService.delete(id);
    }

    // 加分
    @RequestMapping("/plus")
    public void plus(@RequestBody Student student){
        studentService.grade(student, true);
    }

    @RequestMapping("/minus")
    public void minus(@RequestBody Student student){
        studentService.grade(student, false);
    }
}
